package com.example.controller;

import com.example.entity.*;
import com.example.page.Page;
import com.example.service.BuyListService;
import com.example.service.InStoreService;
import com.example.service.StoreService;
import com.example.utils.TokenUtils;
import com.example.utils.WarehouseConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author:tjf
 * @Description:
 */
@RequestMapping("/purchase")
@RestController
public class BuyListController {
    @Autowired
    private BuyListService buyListService;
    @Autowired
    private StoreService storeService;
    @Autowired
    private InStoreService inStoreService;
    @Autowired
    private TokenUtils tokenUtils;
    /**
     * 添加采购单接口url/purchase/purchase-add
     * @param buyList
     * @return
     */
    @RequestMapping("/purchase-add")
    public Result saveBuyList(@RequestBody BuyList buyList){
        // 执行添加采购单的业务方法并响应
        return buyListService.saveBuyList(buyList);
    }

    /**
     * 获取仓库列表接口url/purchase/store-list
     * @return
     */
    @RequestMapping("/store-list")
    public Result getStoreList(){
        List<Store> storeList = storeService.queryAllStore();
        return Result.ok(storeList);
    }

    /**
     * 根据条件查询采购单并分页接口url/purchase/purchase-page-list
     * @return
     */
    @RequestMapping("/purchase-page-list")
    public Result queryBuyListByConditionForPagination(BuyList buyList, Page page){
        // 执行根据条件查询采购单的业务方法
        buyListService.queryBuyListByConditionForPagination(buyList, page);
        // 响应
        return Result.ok(page);
    }

    /**
     * 根据采购单id修改采购单接口/purchase/purchase-update
     * @param buyList
     * @return
     */
    @RequestMapping("/purchase-update")
    public Result modifyBuyListById(@RequestBody BuyList buyList){
        // 执行修改采购单的业务方法
        Result result = buyListService.modifyBuyListById(buyList);

        // 响应
        return result;
    }

    /**
     * 生成入库单接口url/purchase/in-warehouse-record-add
     * @param buyList
     * @param token
     * @return
     */
    @RequestMapping("/in-warehouse-record-add")
    public Result addInStore(@RequestBody BuyList buyList,
                  @RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token){
        // 获取当前登录用户的信息
        CurrentUser currentUser = tokenUtils.getCurrentUser(token);

        // 创建InStore对象并封装数据
        InStore inStore = new InStore();
        inStore.setCreateBy(currentUser.getUserId());
        inStore.setIsIn("0");
        inStore.setProductId(buyList.getProductId());
        inStore.setInNum(buyList.getFactBuyNum());
        inStore.setStoreId(buyList.getStoreId());

        // 执行生成入库单的业务方法
        Result result = inStoreService.saveInStore(inStore, buyList.getBuyId());

        // 响应
        return result;
    }
}
